<template>
  <div>
    <!-- 列表 -->
    <van-address-list
      v-model="chosenAddressId"
      :list="address"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    >
    </van-address-list>

    <!-- 编辑弹框 -->
    <van-dialog v-model="show" title="编辑" show-cancel-button>
      <van-address-edit
        :address-info="modify"
        show-delete
        :show-area="false"
        show-set-default
        show-search-result
        @save="onSave"
        @delete="onDelete"
        @change-detail="onChangeDetail"
      />
    </van-dialog>
  </div>
</template>


<script>


export default {
  name: 'Address',
  computed: {
    address() {
      return this.$store.state.address;
    }
  },
  data() {
    return {
      chosenAddressId: "1",
      show: false,
      modify: {
        name: '123'
      },
    };
  },
  methods: {
    onAdd() {
      this.$router.push({ name: 'add' })
    },
    onEdit(item) {
      this.modify = { ...item, addressDetail: item.address }
      this.show = true;
    },
    onSave(content) {
      console.log(content);
      var obj = {
        id: content.id,
        address: content.addressDetail,
        tel: content.tel,
        name: content.name
      }

      this.$store.commit('MODIFY_ADDRESS', obj)
      this.show = false
    },
    onDelete(content) {
      const { id } = content

      this.$store.commit('DELETE_ADDRESS', id)
      this.show = false

    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "杭州市西湖区"
          }
        ];
      } else {
        this.searchResult = [];
      }
    }
  },
  created() {
     this.$store.dispatch("GET_ADDRESS");
  }
};
</script>